<?php
/*
 * 手机客户端 秀场我的回复
 * 传入参数 
 * 2013-12-011
 */
include "inc/public_header.php";
include "inc/MiniaturePro.class.php";
include "func/user.php";				//用户信息处理函数

//获取用户信息
$uInfoArr = user_getInfoFromUUID();
if(!$uInfoArr){
	echo json_encode(array("success"=>"0","errCode"=>"0"));
	die;
}
$uid = $uInfoArr["uid"];//用户信息主键

$reJson = array();//构建返回值
 
$page			= $_REQUEST["page"]; 			//页数，第一页为0
$pagesize		= $_REQUEST["pagesize"];		//每页记录数
$hd_id			= $_REQUEST["hd_id"];			//活动主键

if(!isset($page)){
	$page = 0;
}
if(!isset($pagesize)){
	$pagesize = 20; //默认每页20条
}

$reJson["success"] = "1";


//评论总数
$sql = "select 
			count(b.uid) as ct
		from 
			new_weituo_zuopin a
			inner join new_weituo_comment b on a.id=b.zpid
		where 
			a.dr='0' and b.dr='0' and a.aflag='1' and b.uid = '".$uid."'
		";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
$db->query($sql);
$db->next_record();
$reJson["count"] = $db->f("ct");

//回复总数（需要统计 评论与回复数的总和）
$sql = "select 
				count(0) as ct 
				from 
				new_weituo_zuopin a 
				inner join new_weituo_huifu b on a.id=b.zpid
LEFT JOIN new_weituo_comment c on c.id=b.pid
		where 
			a.dr='0' and a.aflag='1' and b.dr='0' and c.dr='0' and b.uid='".$uid."' ";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
$db->query($sql);
$db->next_record();
$reJson["count"] = $reJson["count"]+$db->f("ct");

//我的评论
$sql ="
		select 
			 a.id
			,b.id as pid
			,a.aname
			,b.comment
			,b.from_type
			,date_format(b.createtime,'%Y-%m-%d %H:%i:%s') as createtime
		from 
			new_weituo_zuopin a
			inner join new_weituo_comment b on a.id=b.zpid
		where 
			a.dr='0' and b.dr='0' and a.aflag='1' and b.uid = '".$uid."'
	";

if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
$sql = $sql." order by a.id,b.createtime desc ";

$sql = $sql." limit ".($page * ((int)($pagesize/2))).",".((int)($pagesize/2));
$infoArr = array();
$db->query($sql);
while($db->next_record()){
	$infoArr[] = array(
		 "id"			=>	$db->f("pid")			//评论主键
		,"workId"		=>	$db->f("id")			//作品id
		,"imgName"		=>	$db->f("aname")			//作品名称
		,"replay"		=>	$db->f("comment")		//评论内容
		,"date"			=>	$db->f("createtime")	//评论时间
		,"from_type"	=>	$db->f("from_type")			//评论来源
		,"type"			=>	"1"						//类型 1评论  2回复
	);
}

//对应的回复
$sql ="
		select 
			 a.id as zpid
			,b.id
			,a.aname
			,b.acontent
			,date_format(b.ctime,'%Y-%m-%d %H:%i:%s') as ctime
		from 
			new_weituo_zuopin a 
			inner join new_weituo_huifu b on a.id=b.zpid
			LEFT JOIN new_weituo_comment c on c.id=b.pid
		where 
			a.dr='0' and a.aflag='1' and b.dr='0' and c.dr='0' and b.uid='".$uid."' ";
if(isset($hd_id) && $hd_id!=""){
	$sql = $sql." and a.hd_id='".$hd_id."' ";
}
$sql = $sql." order by pid,ctime desc ";
$sql = $sql." limit ".($page * ((int)($pagesize/2))).",".((int)($pagesize/2));
$db->query($sql);
while($db->next_record()){
	$infoArr[] = array(
		 "id"			=>	$db->f("id")		//回复主键
		,"workId"		=>	$db->f("zpid")		//作品id
		,"imgName"		=>	$db->f("aname")		//作品名称
		,"replay"		=>	$db->f("acontent")	//回复内容
		,"date"			=>	$db->f("ctime")		//回复时间
		,"type"			=>	"2"					//类型 1评论  2回复
	);
}
$reJson["list"]=$infoArr;
echo json_encode($reJson);
?>